#!/usr/bin/env bash



# close swap
swapoff -a
swapDevice=`cat /etc/fstab | grep swap | awk '{print $1}'`
sed -i "s#$swapDevice*#\#$swapDevice#g" /etc/fstab
free -m
grep swap /etc/fstab
# close selinux & firewalld
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config
grep '^SELINUX=' /etc/selinux/config
systemctl disable firewalld && systemctl stop firewalld

# donload url: https://share.weiyun.com/55f45dee40a5dcbdd9b9f4d9aa418fc0
unzip kubeadm-rpms.zip ~/

yum install -y docker socat vim ntpdate
#cat << EOF > /etc/docker/daemon.json
# {
 # "exec-opts": ["native.cgroupdriver=systemd"]
#}
#EOF
mkdir -p /etc/docker/certs.d/registry.iboxiao.com:5000/
mv registry.crt /etc/docker/certs.d/registry.iboxiao.com:5000/ca.crt
rpm -ivh kubectl-1.9.1-0.x86_64.rpm kubeadm-1.9.1-0.x86_64.rpm kubernetes-cni-0.6.0-0.x86_64.rpm kubelet-1.9.1-0.x86_64.rpm

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

systemctl enable docker && systemctl start docker
systemctl enable kubelet && systemctl start kubelet


docker images
for image in \
'kube-controller-manager-amd64:v1.9.1' \
'kube-scheduler-amd64:v1.9.1' \
'kube-proxy-amd64:v1.9.1' \
'kube-apiserver-amd64:v1.9.1' \
'pause-amd64:3.0' \
'k8s-dns-sidecar-amd64:1.14.7' \
'k8s-dns-kube-dns-amd64:1.14.7' \
'k8s-dns-dnsmasq-nanny-amd64:1.14.7' \
'etcd-amd64:3.1.10';do
	echo $image
	docker pull "registry.iboxiao.com:5000/google_containers/$image"
	docker tag "registry.iboxiao.com:5000/google_containers/$image" "gcr.io/google_containers/$image"
done
docker images

# hostnamectl set-hostname node2-27

IP=`ip a | grep 172.16.2 | awk '{print $2}'|awk -F '/' '{print $1}'`
hostName=`hostname`
echo "$IP $hostName" >> /etc/hosts
cat /etc/hosts

######################################
#    以上步骤所有节点都需要执行
######################################

#############################
#       master init
#############################
rm -f /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
cat << EOF > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true"
Environment="KUBELET_NETWORK_ARGS=--network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin"
Environment="KUBELET_DNS_ARGS=--cluster-dns=10.96.0.10 --cluster-domain=boxiao.cn"
Environment="KUBELET_AUTHZ_ARGS=--authorization-mode=Webhook --client-ca-file=/etc/kubernetes/pki/ca.crt"
Environment="KUBELET_CADVISOR_ARGS=--cadvisor-port=4194"
Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=systemd"
Environment="KUBELET_CERTIFICATE_ARGS=--rotate-certificates=true --cert-dir=/var/lib/kubelet/pki"
ExecStart=
ExecStart=/usr/bin/kubelet \$KUBELET_KUBECONFIG_ARGS \$KUBELET_SYSTEM_PODS_ARGS \$KUBELET_NETWORK_ARGS \$KUBELET_DNS_ARGS \$KUBELET_AUTHZ_ARGS \$KUBELET_CADVISOR_ARGS \$KUBELET_CGROUP_ARGS \$KUBELET_CERTIFICATE_ARGS \$KUBELET_EXTRA_ARGS
EOF
systemctl daemon-reload

rm -f ./kubeadm.yaml
cat << EOF > ./kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1alpha1
kind: MasterConfiguration
kubernetesVersion: v1.9.1
imageRepository: registry.iboxiao.com:5000/google_containers
networking:
  dnsDomain: boxiao.cn
  serviceSubnet: 10.96.0.0/16
  podSubnet: 10.244.0.0/16

EOF

kubeadm init --config=kubeadm.yaml

export KUBECONFIG=/etc/kubernetes/admin.conf

# ~/.bash_profile
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
curl -okube-flannel.yml https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml

kubectl create -f /var/nas/yaml/nas/pv-nas.yaml
kubectl create -f /var/nas/yaml/nas/pvc-nas.yaml
kubectl create -f /var/nas/yaml/nginx/pv-nginx.yaml
kubectl create -f /var/nas/yaml/nginx/pvc-nginx.yaml
kubectl create -f /var/nas/yaml/nginx/svc-nginx.yaml
kubectl create -f /var/nas/yaml/nginx/rc-nginx.yaml

######################################
#				node add
######################################



